if(tests == undefined) {
    var tests = {};
}

tests.tests = function() {
    return {
        importTests : function() {
            document.location = base_url + 'tests/importTests';
        },
        importResults : function() {
            JsonC.callSfAction('tests/importResults');
        },
        showTest : function(json) {
            JsonC.callSfAction('tests/showTest',{
                id:json.id
            });
        },
        hideTest : function() {
            $('#test-container').html('');
        },
        displayContent : function(json) {
            $('#test-container').html(json.data);
        },
        showError : function(json) {
            JsonC.callSfAction('tests/showError',{id:json.id});
        },
        displayError : function() {
            $('#test-error').dialog(
            {
                title:'Error Message',
                height:400,
                width:1000,
                overlay : {
                    background: "#000",
                    opacity: 0.8
                }
            }
            );
            $('#test-error').show();
        },
        writeInFile : function(testId) {
            JsonC.callSfAction('tests/writeInFile',{
                id:testId
            });
        },
        removeFromDatabase : function(testId) {
            JsonC.callSfAction('tests/removeFromDatabase',{
                id:testId
            });
        },
        runTest : function(testId) {
            if(testId) {
                url = base_url + 'tests/runTest';
                url += '/testId/' + testId;
            }
            else {
                url = base_url + 'tests/runTests';
            }
            window.open(url);
        },
        runClassTests : function(testId) {
            url = base_url + 'tests/runTest';
            url += '/testId/' + testId + '/class/1';
            window.open(url);
        },
        runTestDirs : function() {
            var col = $('#testDirs :checkbox');
            var dirs = "";
            for(var a=0; a < col.length; a++) {
                if($(col[a]).attr('checked')) {
                  if(dirs) dirs += ',';
                  dirs += $(col[a]).val();
                }
            }
            url = base_url + 'tests/runTests';
            url += '/dirs/' + dirs;
            window.open(url);
        },
        runTests : function(dirs) {
            var arDirs = dirs.split(',');
            var html = "<form id='testDirs' onsubmit='return false;'>\n";
            for(i in arDirs) {
                html += '<input type="checkbox" name="dirs" value="'+arDirs[i]+'" checked> ';
                html += arDirs[i]+"<br/>";
            }
            html += "<button onclick='tests.tests.runTestDirs()'>run...</button>";
            html += "</form>";
            UtilC.displayDialog({data:html});
        },
        openTest : function(testId) {
            JsonC.callSfAction('tests/openTest',{
                testId:testId
            });
        },
        showCreateTests : function(testId) {
            var params = null;
            if(testId) {
                params = {
                    testId:testId
                };
            }
            JsonC.callSfAction('tests/showCreateTests',params);
        },
        displayCreateTests : function(json) {
            UtilC.displayDialog(
            {
                data:json.template,
                width:'950px',
            }
            );
            $.metadata.setType("attr");
            $("#create-form-tree").tree(
            {
                data :
                {
                    type : "json",
                    json: json.treeSource
                },
                rules :
                {
                    metadata: "extra",
                    use_inline : true
                },
                callback :
                {
                    onchange: tests.tree.onchange
                }
            }
            );
        },
        createTests : function(json) {
            JsonC.submitForSfAction('tests/createTests',{},'create-form','create-form-loading');
        }
    };
}();